﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entity;
using System.Data;

namespace Data
{
    public class Orders_Data
    {
        SqlConnection con;
        SqlCommand comn;
        SqlDataAdapter da;
        DataSet ds;

        public bool Insert_orders(Orders_Entity orders)
        {
            try
            {
                con = Connection.StartConnection();

                comn = new SqlCommand("ordersadd", con);
                con.Open();
                comn.CommandType = CommandType.StoredProcedure;
                comn.Parameters.Add(new SqlParameter("@cusid", orders.cusID));
                comn.Parameters.Add(new SqlParameter("@receivename", orders.receiveName));
                
                 comn.Parameters.Add(new SqlParameter("@receiveaddress", orders.receiveAddress));
                comn.Parameters.Add(new SqlParameter("@receiverphone", orders.receivePhone));
                comn.Parameters.Add(new SqlParameter("@senddate", orders.Senddate));
                comn.Parameters.Add(new SqlParameter("@reveivedate", orders.Receivedate));
                comn.Parameters.Add(new SqlParameter("@weight", orders.Weight));
                comn.Parameters.Add(new SqlParameter("@provinceid", orders.provinceID));
                comn.Parameters.Add(new SqlParameter("@serid", orders.serID));
                comn.Parameters.Add(new SqlParameter("@price", orders.Price));
                comn.Parameters.Add(new SqlParameter("@status", orders.Status));

                comn.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.CloseConnection();
            }

            return true;
        }

        public DataTable Load()
        {
            con = Connection.StartConnection();
            comn = new SqlCommand("ordersbind", con);
            comn.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comn);

            ds = new DataSet();
            try
            {
                da.Fill(ds, "orders");
                return ds.Tables["orders"];
            }
            catch
            {
                throw;
            }
            finally
            {
                ds.Dispose();
                da.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        public int Delete_orders(int orderid)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("ordersdelete", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@orderid",orderid);
                return comn.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                comn.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        public int Update_orders(int orderid, int cusid,string receivename,string receiveaddress,int receivephone,DateTime senddate,DateTime receivedate,int weight,int provinceid,int serid,int price,string status )
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("ordersupdate", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@orderid", orderid);
                comn.Parameters.AddWithValue("@cusid", cusid);
                
                comn.Parameters.AddWithValue("@receivename", receivename);
                comn.Parameters.AddWithValue("@receiveaddress", receiveaddress);
                comn.Parameters.AddWithValue("@receiverphone", receivephone);
                comn.Parameters.AddWithValue("@senddate", senddate);
                comn.Parameters.AddWithValue("@reveivedate", receivedate);
                comn.Parameters.AddWithValue("@weight", weight);
                comn.Parameters.AddWithValue("@provinceid", provinceid);
                comn.Parameters.AddWithValue("@serid", serid);
                comn.Parameters.AddWithValue("@price", price);
                comn.Parameters.AddWithValue("@status", status);
                return comn.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                Connection.CloseConnection();
            }

        }
    }
}
